import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class YunShijieSimCardClient {

    public static void main(String [] args) throws IOException {
        String url = "http://120.27.236.1/palette/#/login";
        Connection.Response res = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
                .method(Connection.Method.GET)
                .timeout(10000)
                .execute();
         if(res.statusCode() != 200){
             System.out.println("访问失败");
             return;
         }
         String resText = res.body();
//        System.out.println(resText);
        String regex = "pid:\"([^\\(\\)\"]+)\",";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(resText);
        if(m.find()){
            System.out.println(m.group(1));
        }

    res = Jsoup.connect("http://120.27.236.1/user?action=login")
            .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
            .data("userAccount", "13798583283", "password", "Xlkc13798583283")
            .header("EagleEye-pAppName","ctel8hp9j6@3a5d8e53fbb1f63")
            .header("EagleEye-SessionID","hsljplCedLRv6t6atpCI7R4czOXg")
            .header("EagleEye-TraceID","b97f760316922001092301003b1f63")
            .ignoreContentType(true)
            .cookies(res.cookies())
                .method(Connection.Method.POST)
                .timeout(10000)
                .execute();
        JSONObject resJson = JSON.parseObject(res.body());
        if(resJson.getIntValue("code") != 200 || !resJson.getBooleanValue("success") ){
            System.out.println("登录失败");
            return ;
        }
        res = Jsoup.connect("http://120.27.236.1/user?action=hasLogin")
                .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
                .header("EagleEye-pAppName","ctel8hp9j6@3a5d8e53fbb1f63")
                .header("EagleEye-SessionID","hsljplCedLRv6t6atpCI7R4czOXg")
                .header("EagleEye-TraceID","b97f760316922001092301003b1f63")
                .ignoreContentType(true)
                .cookies(res.cookies())
                .method(Connection.Method.POST)
                .timeout(10000)
                .execute();

          resJson = JSON.parseObject(res.body());
        if(resJson.getIntValue("code") != 200 || !resJson.getBooleanValue("success") ){
            System.out.println("登录失败");
            return ;
        }
        System.out.println();


        Map<String, Object> params = Maps.newHashMap();
        params.put("excludeRetired",false);
        params.put("currentPage",1);
        params.put("rowsPerPage",15);
        Map<String,Object> cardCondition = Maps.newHashMap();
        cardCondition.put("iccids", new JSONArray());
        cardCondition.put("carrier", "cmcc");
        params.put("cardCondition",cardCondition);
        res = Jsoup.connect("http://120.27.236.1/dc/card/list")
                .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
                .header("EagleEye-pAppName","ctel8hp9j6@3a5d8e53fbb1f63")
                .header("EagleEye-SessionID","hsljplCedLRv6t6atpCI7R4czOXg")
                .header("EagleEye-TraceID","b97f760316922001092301003b1f63")
                .ignoreContentType(true)
                .cookies(res.cookies())
                .method(Connection.Method.POST)
                .timeout(10000)
                .execute();
        System.out.println(res.body());
    }
}
